﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, Procedure SplitEntityName

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO

create table #TestNames (
	TestNo              integer not null           identity (1, 1),
	FullEntityName      nvarchar(512),
	Level0Name          nvarchar(128),
	Level1Name          nvarchar(128),
	Level2Name          nvarchar(128),
	ErrorCode           integer,
	primary key (TestNo)
);
GO

insert into #TestNames (FullEntityName, Level0Name, Level1Name, Level2Name, ErrorCode)
values
	('Single', 'Single', null, null, null),
	('Two.Names', 'Two', 'Names', null, null),
	('Three.Part.Name', 'Three', 'Part', 'Name', null),
	('Name.With.Four.Parts', null, null, null, 740900),
	(null, null, null, null, null),
	('', null, null, null, null),
	('[Quoted].[Name]', 'Quoted', 'Name', null, null),
	('.PartOne.PartTwo', null, null, null, 740900),
	('One..Three', null, null, null, 740900),
	('One..', null, null, null, 740900),
	('One very long name without any dots 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890', null, null, null, 740900);
GO

declare @TestNo as integer;
declare @FullEntityName as nvarchar(512);
declare @Level0Name as nvarchar(128);
declare @Level1Name as nvarchar(128);
declare @Level2Name as nvarchar(128);
declare @ErrorCode as integer;

set @TestNo = (select min(TestNo) from #TestNames);

while
	@TestNo is not null
begin
	
	set @FullEntityName = (select FullEntityName from #TestNames where TestNo = @TestNo);

	set @Level0Name = null;
	set @Level1Name = null;
	set @Level2Name = null;
	set @ErrorCode = null;

	begin try;
		execute VanguardDocumentation.SplitEntityName
			@FullEntityName, @Level0Name out, @Level1Name out, @Level2Name out;
	end try
	begin catch
		set @ErrorCode = error_number();
	end catch;

	if
		exists (
			select * from #TestNames where TestNo = @TestNo
			except
			select @TestNo, @FullEntityName, @Level0Name, @Level1Name, @Level2Name, @ErrorCode
		)
	begin
		print 'SplitEntityName, Failed test no ' + rtrim(@TestNo);
		print '	' + coalesce(quotename(@FullEntityName, ''''), 'null')
			+ ' >>> ' + coalesce(quotename(@Level0Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Name, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Name, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end;

	set @TestNo = (select min(TestNo) from #TestNames where TestNo > @TestNo);

end;
GO

drop table #TestNames;
GO

set nocount off;
GO




